package com.zhc.dejingapi.utils.qing;

//import com.alibaba.fastjson.JSON;

import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.appfield.QingFieldResultMainBase;
import com.zhc.dejingapi.pojo.qingflow.downdata.QingDownDataResultMainBase;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.utils.HttpClientUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Component
public class QingflowAPIUtils {
    public  static  QingFieldResultMainBase  GetAppFieldInfo(QingflowApiInfo qingflowApiInfo, String appKey, String strBody) throws Exception{
        QingFieldResultMainBase mainBase=null;
        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "app/"+ appKey + "/apply/filter";
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack= HttpClientUtil.doPost(strUrl,strBody,mapHead);
            //log.info("返回结果："+strBack);
            //mainBase= JSON.parseObject(strBack,QingFieldResultMainBase.class);
            Gson gson = new Gson();
            mainBase= gson.fromJson(strBack, QingFieldResultMainBase.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }
        return mainBase;


    }

    public  static QingDownDataResultMainBase GetAppDataInfo(QingflowApiInfo qingflowApiInfo, String appKey, String strBody) throws Exception{
        QingDownDataResultMainBase mainBase=null;
        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "app/"+ appKey + "/apply/filter";
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack= HttpClientUtil.doPost(strUrl,strBody,mapHead);
            //log.info("返回结果："+strBack);
            //mainBase= JSON.parseObject(strBack,QingDownDataResultMainBase.class);
            Gson gson = new Gson();
            mainBase= gson.fromJson(strBack, QingDownDataResultMainBase.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }
        return mainBase;


    }


    public  static QingQueryActionResultBase GetRequestResult(QingflowApiInfo qingflowApiInfo, String requestId) throws Exception{

        QingQueryActionResultBase actionResultBase=null;
        //http://39.185.158.205:1080/openApi/operation/dabf18fe-3ce2-4c06-b5b7-68ef76b0a58f
        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);


            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "operation/"+requestId;
            //doPost(String url,  String json, Map<String,String> headers)
            System.out.println(strUrl);
            String strBack= HttpClientUtil.doGet(strUrl,mapHead);
            //log.info("返回结果："+strBack);
            //actionResultBase= JSON.parseObject(strBack,QingQueryActionResultBase.class);
            Gson gson = new Gson();
            actionResultBase= gson.fromJson(strBack, QingQueryActionResultBase.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }

        return  actionResultBase;
    }

    public  static QingAddDataResultMain AddDataToQing(QingflowApiInfo qingflowApiInfo, String appKey, String strBody) throws Exception{
        QingAddDataResultMain resultMain=null;

        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "app/"+ appKey + "/apply";
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack= HttpClientUtil.doPost(strUrl,strBody,mapHead);
            //log.info("返回结果："+strBack);
            //resultMain= JSON.parseObject(strBack,QingAddDataResultMain.class);
            Gson gson = new Gson();
            resultMain= gson.fromJson(strBack, QingAddDataResultMain.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }

        return  resultMain;

    }

    public  static QingAddDataResultMain UpdDataToQing(QingflowApiInfo qingflowApiInfo, String appKey, String strBody, String strApplyId) throws Exception{
        QingAddDataResultMain resultMain=null;

        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            //mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "apply/"+ strApplyId;
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack= HttpClientUtil.doPost(strUrl,strBody,mapHead);
            //log.info("返回结果："+strBack);
            //resultMain= JSON.parseObject(strBack,QingAddDataResultMain.class);
            Gson gson = new Gson();
            resultMain= gson.fromJson(strBack, QingAddDataResultMain.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }

        return  resultMain;

    }

    public  static QingAddDataResultMain DelDataToQing(QingflowApiInfo qingflowApiInfo, String appKey, String strBody) throws Exception{
        QingAddDataResultMain resultMain=null;

        try{
            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.FetchUrl();
            strUrl += "app/"+ appKey + "/apply";
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack=HttpClientUtil.doDel(strUrl,mapHead,strBody);
            //log.info("返回结果："+strBack);
            //resultMain= JSON.parseObject(strBack,QingAddDataResultMain.class);
            Gson gson = new Gson();
            resultMain= gson.fromJson(strBack, QingAddDataResultMain.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }

        return  resultMain;

    }

    public  static QingAddDataResultMain UpdDataToQingByQSource(QingflowApiInfo qingflowApiInfo, String qSourceKey, String strBody) throws Exception{
        QingAddDataResultMain resultMain=null;

        try{
//            String strQingAccessToken=accessTokenUtils.getAccessToken(qingflowApiInfo);

//            Map<String, String> mapHead =new HashMap<>();
            //dicHead.Add("accessToken", strQingAccessToken);
//            mapHead.put("accessToken",strQingAccessToken);
            //dicHead.Add("userId", qingFlowAPIInfo.userId);
            //mapHead.put("userId", qingflowApiInfo.getUserId());

            //String appKey="7llkk74s1c01";

            String strUrl=qingflowApiInfo.getServerUrl();
            strUrl += "api/qsource/"+ qSourceKey;
            //doPost(String url,  String json, Map<String,String> headers)
            String strBack= HttpClientUtil.doPost(strUrl,strBody,null);
            //log.info("返回结果："+strBack);
            //resultMain= JSON.parseObject(strBack,QingAddDataResultMain.class);
            Gson gson = new Gson();
            resultMain= gson.fromJson(strBack, QingAddDataResultMain.class);
        }
        catch (Exception e) {
            //log.error("获取应用字段信息失败："+e.getMessage());
            e.printStackTrace();
            throw e;
        }

        return  resultMain;

    }




}
